<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- <script src="../JS/Allcheck.js"></script> -->
    <script src="../jquery/jquery-3.6.0.min.js"></script>
    <style type="text/css">
        table {
            width: 1000px;
            /* height: 300px; */
            border-collapse: collapse;
            table-layout: fixed;
            text-align: center;
            font-size: 18px;
            margin: 0 auto;
        }

        a {
            text-decoration: none;
            color: black;
        }

        tr {
            height: 50px;
        }

        .check {
            width: 20px;
            height: 20px;
        }

        .checkOnly {
            width: 20px;
            height: 20px;
        }

        .empty {
            font-size: 25px;
            position: fixed;
            top: 45%;
            left: 45%;
            display: none;
        }

        .empty a {
            color: pink;
        }

        .empty a:hover {
            text-decoration: underline;
        }
    </style>
</head>

<body>
    <div class="empty">
        购物车空空如也，<a href="javascript:void(0);">快去选购吧</a>
    </div>
    <table border="2px solid #ccc" id="table">
        <thead>
            <th>
                <input type="checkbox" class="checkOnly" style="vertical-align:middle;margin-right:20px;">全选
            </th>
            <th>序号</th>
            <th>商品名称</th>
            <th>数量</th>
            <th>单价</th>
            <th>小计</th>
            <th>操作</th>
        </thead>
        <tbody>
            <tr>
                <td>
                    <input type="checkbox" class="check">
                </td>
                <td class="num">1</td>
                <td>烤煎饼</td>
                <td>
                    <span>
                        <input type="button" value="-" class="reduces">
                        <span class="span">1</span>
                        <input type="button" value="+" class="adds">
                    </span>
                </td>
                <td>单价：
                    <span class="price">2</span>
                </td>
                <td>
                    小计：
                    <span class="prices">2</span>
                </td>
                <td>
                    <a href="#" class="del">删除</a>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="checkbox" class="check">
                </td>
                <td class="num">2</td>
                <td>珍珠奶茶</td>
                <td>
                    <span>
                        <input type="button" value="-" class="reduces">
                        <span class="span">1</span>
                        <input type="button" value="+" class="adds">
                    </span>
                </td>
                <td>单价：
                    <span class="price">4</span>
                </td>
                <td>
                    小计：
                    <span class="prices">4</span>
                </td>
                <td>
                    <a href="#" class="del">删除</a>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="checkbox" class="check">
                </td>
                <td class="num">3</td>
                <td>水煮鱼</td>
                <td>
                    <span>
                        <input type="button" value="-" class="reduces">
                        <span class="span">1</span>
                        <input type="button" value="+" class="adds">
                    </span>
                </td>
                <td>单价：
                    <span class="price">20</span>
                </td>
                <td>
                    小计：
                    <span class="prices">20</span>
                </td>
                <td>
                    <a href="#" class="del">删除</a>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="checkbox" class="check">
                </td>
                <td class="num">4</td>
                <td>蛋糕</td>
                <td>
                    <span>
                        <input type="button" value="-" class="reduces">
                        <span class="span">1</span>
                        <input type="button" value="+" class="adds">
                    </span>
                </td>
                <td>单价：
                    <span class="price">50</span>
                </td>
                <td>
                    小计：
                    <span class="prices">50</span>
                </td>
                <td>
                    <a href="#" class="del">删除</a>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="checkbox" class="check">
                </td>
                <td class="num">5</td>
                <td>土豆片</td>
                <td>
                    <span>
                        <input type="button" value="-" class="reduces">
                        <span class="span">1</span>
                        <input type="button" value="+" class="adds">
                    </span>
                </td>
                <td>单价：
                    <span class="price">5</span>
                </td>
                <td>
                    小计：
                    <span class="prices">5</span>
                </td>
                <td>
                    <a href="#" class="del">删除</a>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="checkbox" class="check">
                </td>
                <td class="num">6</td>
                <td>蛋黄派</td>
                <td>
                    <span>
                        <input type="button" value="-" class="reduces">
                        <span class="span">1</span>
                        <input type="button" value="+" class="adds">
                    </span>
                </td>
                <td>单价：
                    <span class="price">5.5</span>
                </td>
                <td>
                    小计：
                    <span class="prices">5.5</span>
                </td>
                <td>
                    <a href="#" class="del">删除</a>
                </td>
            </tr>
            <tr>
                <td colspan="7" class="talast">
                    <span>商品一共
                        <span class="goods_num" style="color:red;font-size:20px;">0</span> 件; 共计花费
                        <span class="pricetal" style="color:red;font-size:20px;">0</span> 元; 其中最贵的商品单价是
                        <span class="pricest" style="color:red;font-size:20px;">0</span> 元</span>
                </td>
            </tr>
        </tbody>
    </table>
</body>

<script>

    //  全选

    $('.checkOnly').click(function () {

        // 单选的状态跟随全选checked的状态
        $('.check').prop('checked', $(this).prop('checked'))
        getTotal();
        compare();
    })

    //  单选  需要判断全部的单选
    $('.check').click(function () {
        getTotal();
        isAllChecked();
        compare();
    })


    //  加号
    $('.adds').click(function () {
        //  取加号前面的着  
        var num = $(this).prev().text();
        num++;

        $(this).prev().text(num);
        //  单价小计变更
        var price = $(this).parents('td').next().find("span").text();

        var prices = (price * num).toFixed(2);
        // console.log(price, prices);
        //  小计等于 单价乘以数量
        $(this).parents('td').next().next().find('span').text(prices);
        getTotal();
        compare();

    })

    //  减号

    $('.reduces').click(function () {
        var num = $(this).next().text();

        num--;
        // 判读 num  =1 时停止
        if (num < 1) return;

        $(this).next().text(num);
        //  小计变更
        var price = $(this).parents('td').next().find("span").text();

        var prices = (price * num).toFixed(2);
        // console.log(price, prices);
        //  小计等于 单价乘以数量
        $(this).parents('td').next().next().find('span').text(prices);
        getTotal();
        compare();

    })

    // 删除
    $('.del').click(function () {
        if (confirm("确认删除本商品")) {
            $(this).parents('tr').remove();
        }
        getTotal();
        compare();

    })

    //  总计 结算
    function getTotal() {
        //  初始值
        var goods_num = 0;
        var pricetal = 0;
        //   找到 选中状态下的 商品数量和小计

        $('.check:checked').parents('tr').each(function () {
            var num = $(this).find('.span').text() * 1;
            var prices = $(this).find('.prices').text() * 1;
            goods_num += num;
            pricetal += prices;
            // console.log(goods_num, pricetal);


        })
        //  赋值反还给 结算
        $('.goods_num').text(goods_num);
        $('.pricetal').text(pricetal);



    }


    //  选中 状态下 判断 最大单价

    function compare() {
            let temp = 0;
            for (let i = 0; i < $(".price").length; i++) { //循环单价
                if ($(".check")[i].checked == true) {
                    var temps = parseFloat($(".price")[i].innerHTML);
                    if (temps > temp) {
                        temp = temps;
                    }
                }
            };
            $(".pricest").html(temp);
        }

    //  判断  选中的状态

    function isAllChecked() {
        // 先判断单选项 有没有   没有选中  的状态  存在true  不存在 false       
        var flag = $('.check').is(":not(:checked)");
        //  根据单选的状态 判读要不要给全选加选中状态  第一看check没有  就false  第二判断 单选状态有没有没有选中的 
        $('.checkOnly').prop('checked', $('.check').length == 0 ? false : !flag)

    }

</script>

</html>